﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using TCS.DBUtility;//Please add references
namespace TCS.DAL
{
	/// <summary>
	/// 数据访问类:Clients
	/// </summary>
	public partial class Clients
	{
		public Clients()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("Id", "Clients"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Clients");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(TCS.Model.Clients model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Clients(");
			strSql.Append("TypeId,ClientName,Remark,Picture,AddDate,Clicks,Verify)");
			strSql.Append(" values (");
			strSql.Append("@TypeId,@ClientName,@Remark,@Picture,@AddDate,@Clicks,@Verify)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeId", SqlDbType.Int,4),
					new SqlParameter("@ClientName", SqlDbType.VarChar,50),
					new SqlParameter("@Remark", SqlDbType.Text),
					new SqlParameter("@Picture", SqlDbType.VarChar,50),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@Clicks", SqlDbType.Int,4),
					new SqlParameter("@Verify", SqlDbType.Bit,1)};
			parameters[0].Value = model.TypeId;
			parameters[1].Value = model.ClientName;
			parameters[2].Value = model.Remark;
			parameters[3].Value = model.Picture;
			parameters[4].Value = model.AddDate;
			parameters[5].Value = model.Clicks;
			parameters[6].Value = model.Verify;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(TCS.Model.Clients model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Clients set ");
			strSql.Append("TypeId=@TypeId,");
			strSql.Append("ClientName=@ClientName,");
			strSql.Append("Remark=@Remark,");
			strSql.Append("Picture=@Picture,");
			strSql.Append("AddDate=@AddDate,");
			strSql.Append("Clicks=@Clicks,");
			strSql.Append("Verify=@Verify");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeId", SqlDbType.Int,4),
					new SqlParameter("@ClientName", SqlDbType.VarChar,50),
					new SqlParameter("@Remark", SqlDbType.Text),
					new SqlParameter("@Picture", SqlDbType.VarChar,50),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@Clicks", SqlDbType.Int,4),
					new SqlParameter("@Verify", SqlDbType.Bit,1),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.TypeId;
			parameters[1].Value = model.ClientName;
			parameters[2].Value = model.Remark;
			parameters[3].Value = model.Picture;
			parameters[4].Value = model.AddDate;
			parameters[5].Value = model.Clicks;
			parameters[6].Value = model.Verify;
			parameters[7].Value = model.Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Clients ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
			parameters[0].Value = Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Clients ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public TCS.Model.Clients GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Id,TypeId,ClientName,Remark,Picture,AddDate,Clicks,Verify from Clients ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
			parameters[0].Value = Id;

			TCS.Model.Clients model=new TCS.Model.Clients();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"]!=null && ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["TypeId"]!=null && ds.Tables[0].Rows[0]["TypeId"].ToString()!="")
				{
					model.TypeId=int.Parse(ds.Tables[0].Rows[0]["TypeId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ClientName"]!=null && ds.Tables[0].Rows[0]["ClientName"].ToString()!="")
				{
					model.ClientName=ds.Tables[0].Rows[0]["ClientName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Remark"]!=null && ds.Tables[0].Rows[0]["Remark"].ToString()!="")
				{
					model.Remark=ds.Tables[0].Rows[0]["Remark"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Picture"]!=null && ds.Tables[0].Rows[0]["Picture"].ToString()!="")
				{
					model.Picture=ds.Tables[0].Rows[0]["Picture"].ToString();
				}
				if(ds.Tables[0].Rows[0]["AddDate"]!=null && ds.Tables[0].Rows[0]["AddDate"].ToString()!="")
				{
					model.AddDate=DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Clicks"]!=null && ds.Tables[0].Rows[0]["Clicks"].ToString()!="")
				{
					model.Clicks=int.Parse(ds.Tables[0].Rows[0]["Clicks"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Verify"]!=null && ds.Tables[0].Rows[0]["Verify"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["Verify"].ToString()=="1")||(ds.Tables[0].Rows[0]["Verify"].ToString().ToLower()=="true"))
					{
						model.Verify=true;
					}
					else
					{
						model.Verify=false;
					}
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,TypeId,ClientName,Remark,Picture,AddDate,Clicks,Verify ");
			strSql.Append(" FROM Clients ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Id,TypeId,ClientName,Remark,Picture,AddDate,Clicks,Verify ");
			strSql.Append(" FROM Clients ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Clients";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

